struct Gc_SbarAmmoGrid ui {
	// Ammo grids for the bars:
	private TextureId					bg_sprite;
	private int							bg_sprite_width;
	private int							bg_sprite_height;
	private int							extra_bg_size;
	private int							bg_last_sprite_height;
	private int							bars_per_bg;
	
	void InitFromAmmoBars (String sprite_name, int bar_count, Gc_SbarAmmoBars bars) {
		bars_per_bg = max(1, bar_count);
		
		LoadBackSprite(sprite_name);
		
		// Calculate amount of copies necessary to fit all our ammo bars:
		int bar_count = bars.Count();
		extra_bg_size = max(0, bar_count / bars_per_bg);
		
		int extra_bars = bar_count % bars_per_bg;
		double height_f = 1.0 * extra_bars / bars_per_bg;
		
		bg_last_sprite_height = height_f * bg_sprite_height;
	}
	
	private void LoadBackSprite (String sprite_name) {
		// Get bar grid and height:
		bg_sprite			= TexMan.CheckForTexture(sprite_name, TexMan.type_any);
		vector2 size		= TexMan.GetScaledSize(bg_sprite);
		bg_sprite_width		= size.x;
		bg_sprite_height	= size.y;
	}
	
	void DrawFull (Gc_StatusBar bar, double alpha = 1) const {
		// Draw copies:
		int x = 0;
		int y = 0;
		
		for(int i = 0; i < extra_bg_size; i++) {
			bar.DrawTexture(bg_sprite, (x, y), bar.DI_ITEM_LEFT_TOP, alpha: alpha);
			y += bg_sprite_height;
		}
		
		// Draw last one clipped:
		bar.SetClipRect(x, y, bg_sprite_width, bg_last_sprite_height);
		bar.DrawTexture(bg_sprite, (x, y), bar.DI_ITEM_LEFT_TOP, alpha: alpha);
	}
}

class Gc_SbarAmmoBars {
	// Ammo bars:
	private Array <class <Ammo> >		type;
	private Array <TextureId>			sprite;
	private Array <TextureId>			mini_sprite;
	private TextureId					bg_sprite;
	private Array <int>					sprite_width;
	private Array <int>					sprite_height;
	private int							size;
	
	// kd:
	// The coveted ammo bars that annoyed pillow so much.
	
	const bar_border_size		= 2;
	const ammo_count_x_offset	= 51;
	
	int Count () const {
		return size;
	}
	
	// kd:
	// All about ammo data. We need to be exact about which ammo types to draw,
	// due to cross-iwad shenanigans. Do the setup cleanly and the rest will be
	// simple as can be.
	
	void Load (String bg_sprite_name) {
		bg_sprite = TexMan.CheckForTexture(bg_sprite_name, TexMan.type_any);
		type.clear();
		sprite.clear();
		sprite_width.clear();
		sprite_height.clear();
		size = 0;
	}
	
	void Add (
	class <Ammo>	ammo_type,
	String			sprite_name,
	String			mini_sprite_name) {
		TextureId ammo_sprite = TexMan.CheckForTexture(sprite_name, TexMan.type_any);
		TextureId m_sprite = TexMan.CheckForTexture(mini_sprite_name, TexMan.type_any);
		vector2 sprite_size = TexMan.GetScaledSize(ammo_sprite);
		
		type.					push(ammo_type);
		sprite_width.			push(sprite_size.x);
		sprite_height.			push(sprite_size.y);
		sprite.					push(0); // lol
		sprite [size] =			ammo_sprite;
		mini_sprite.			push(0);
		mini_sprite [size] =	m_sprite;
		
		size = type.size();
	}
	
	void Copy (Gc_SbarAmmoBars other) {
		type.			copy(other.type);
		sprite.			copy(other.sprite);
		mini_sprite.	copy(other.mini_sprite);
		sprite_width.	copy(other.sprite_width);
		sprite_height.	copy(other.sprite_height);
		bg_sprite	= other.bg_sprite;
		size		= other.size;
	}
	
	ui void DrawWithAmounts (
	Gc_Statusbar	bar,
	HudFont			amount_font,
	int				font_color,
	double			alpha		= 1,
	double			text_alpha	= 1) const {
		int amount;
		int max_amount;
		
		int x = bar_border_size;
		int y = bar_border_size;
		
		for(int i = 0; i < size; i++) {
			[amount,  max_amount] = bar.GetAmount(type [i]);
			
			bar.DrawTexture(bg_sprite, (x, y), bar.DI_ITEM_LEFT_TOP, alpha);
			bar.DrawRightBarAmount(
				sprite [i],
				x, y,
				sprite_width [i],
				sprite_height [i],
				amount, max_amount,
				alpha);
			
			bar.RestoreClipRectFromCache();
			
			if(0 < amount) {
				bar.DrawRightNum(
					amount,
					x + ammo_count_x_offset - 1, y,
					amount_font, font_color, text_alpha);
			}
			
			y += bar_border_size * 2 + sprite_height [i];
		}
	}
	
	ui void DrawMiniWithAmounts (
	Gc_Statusbar	bar,
	HudFont			amount_font,
	int				font_color,
	int				num_offset	= 16,
	double			alpha		= 1,
	double			text_alpha	= 1) const {
		int amount;
		int max_amount;
		
		int x = bar_border_size;
		int y = bar_border_size;
		
		for(int i = 0; i < size; i++) {
			[amount,  max_amount] = bar.GetAmount(type [i]);
			
			if(0 < amount) {
				bar.DrawTexture(mini_sprite [i], (x, y), bar.DI_ITEM_LEFT_TOP, alpha);
				bar.DrawLeftNum(
					amount,
					x + num_offset, y,
					amount_font, font_color, text_alpha);
				
				y += bar_border_size * 2 + sprite_height [i];
			}
		}
	}
	
	ui void DrawFilled (Gc_StatusBar bar, double alpha = 1) const {
		int amount;
		int max_amount;
		
		int x = bar_border_size;
		int y = bar_border_size;	
		bar.RestoreClipRectFromCache();
		
		for(int i = 0; i < size; i++) {
			// TODO: add alpha up properly here
			bar.DrawTexture(bg_sprite, (x, y), bar.DI_ITEM_LEFT_TOP, alpha);
			bar.DrawTexture(sprite [i], (x, y), bar.DI_ITEM_LEFT_TOP, alpha);
			y += bar_border_size * 2 + sprite_height [i];
		}
	}
	
	ui void Dim (Gc_StatusBar bar, Color fill_color) const {
		int x = bar_border_size;
		int y = bar_border_size;
		bar.RestoreClipRectFromCache();
		
		for(int i = 0; i < size; i++) {
			bar.Fill(fill_color, x, y, sprite_width [i], sprite_height [i]);
			y += bar_border_size * 2 + sprite_height [i];
		}
	}
}